package com.fsq.service.impl;

import com.fsq.beans.vo.request.EmployeePost;
import com.fsq.beans.vo.request.EmployeePut;
import com.fsq.beans.vo.response.EmployeeRes;
import com.fsq.dao.EmployeeDao;
import com.fsq.entity.Employee;
import com.fsq.service.EmployeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author fsq
 * @date 2020/07/27
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeDao employeeDao;

    @Override
    public List<EmployeeRes> listEmployees() {
        List<EmployeeRes> result = new ArrayList<EmployeeRes>();
        List<Employee> employees = employeeDao.listEmployees();
        employees.forEach(e -> {
            EmployeeRes data = new EmployeeRes();
            BeanUtils.copyProperties(e, data);
            result.add(data);
        });

        return result;
    }

    @Override
    public EmployeeRes getById(Integer id) {
        EmployeeRes result = new EmployeeRes();
        BeanUtils.copyProperties(employeeDao.getById(id), result);
        return result;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(EmployeePut employeePut) {
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeePut, employee);
        employeeDao.update(employee);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insert(EmployeePost employeePost) {
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeePost, employee);
        employeeDao.insertSelective(employee);

    }

    @Override
    public void delete(Integer id) {
        employeeDao.delete(id);
    }
}
